/*******************************************************************************
 * 
 * Copyright:
 * 
 * License:
 * 
 * Authors: SALMON Charles
 * 
 ******************************************************************************/

/*******************************************************************************
 * 
 ******************************************************************************/

qx.Class.define("bip.ui.form.survey.AnswerFieldRadioGroup",
{
  extend : qx.ui.core.Widget,
  
  implement : [
               qx.ui.form.INumberForm,
               qx.ui.form.IForm
             ],
  include : [
     qx.ui.form.MForm
  ],
  
  /**
   * @param choices {Array} A set of arrays; each arrays must contains the possible value and it's label
   */
  construct : function(choices)
  {
    this.base(arguments);
    
    this.__checkboxes = [];
    for (var i=0;i<choices.length;i++) {
    	var value = choices[i][0];
    	var label = choices[i][1];
    	if (label == null) {label = value;}
    	var cb = new qx.ui.form.CheckBox(label);
    	cb.setModel(value);
    	cb.setValue(false);
    	cb.setUserData("label",label);
			this.__checkboxes.push(cb);
	}
    // initialize the layout
    var layout = new qx.ui.layout.VBox(5);
    this._setLayout(layout);
    
    // create the widgets
    this._createChildControl("answer-value");
    this._createChildControl("answer-change-value");
    this._createChildControl("radiogroup");
  },

  properties :
  {
  	appearance :
    {
      refine : true,
      init : "answer-field-radiogroup"
    },
    
    value :
    {
      check : "Integer",
      nullable : true,
      event : "changeValue",
      apply : "_applyValue"
    }
   
  },

  members :
  { 
  	__checkboxes: null,
  	
    // overridden
    _createChildControlImpl : function(id)
    {
      var control;

      switch(id)
      { 
      	case "answer":
      		control = new qx.ui.container.Composite(new qx.ui.layout.HBox(10));
      		this._add(control);
      		 control.addListener("click",function(evt) {
             	this.getChildControl("answer").exclude();
             	this.getChildControl("radiogroup").show();
             },this);
      		//control.exclude();
      		break;
      		
      	case "answer-value":
            control = new qx.ui.basic.Atom("answer-value","icon/16/actions/dialog-apply.png");
            control.setAnonymous(true);
            control.setRich(true);
            control.setAllowGrowX(true);
            this.getChildControl("answer").add(control);
            break;
            
      	case "answer-change-value":
            control = new qx.ui.basic.Label(this.tr("(click to change the answer)"));
            control.setAnonymous(true);
            control.setAllowGrowX(true);
           
            this.getChildControl("answer").add(control);
            break;
      	
      	case "radiogroup":
      		// Build radiogroup
      		control = new qx.ui.form.RadioButtonGroup(new qx.ui.layout.VBox());
      		control.getRadioGroup().setAllowEmptySelection(true);
      		// Build check-boxes
      		for (var i=0;i<this.__checkboxes.length;i++) {
      			control.add(this.__checkboxes[i]);
      		}
      		control.addListener("changeSelection",function(evt) {
      			var selection = evt.getData();
      			if (selection.length > 0) {
      				this.setValue(selection[0].getModel());
      			}
      		},this);
      		//control.setWidth(100);
      		control.setAnonymous(true);
          this._add(control);
          break;
      }

      return control || this.base(arguments, id);
    },
    
    _applyValue: function(value,old) {
    	var cb = this.getCheckBoxByValue(value);
    	if (cb != null) {
    		cb.setValue(true);
    		this.getChildControl("answer").show();
    		this.getChildControl("answer-value").setLabel(cb.getUserData("label"));
    		this.getChildControl("radiogroup").exclude();
    	} else {
    		this.getChildControl("answer").exclude();
    		this.getChildControl("radiogroup").show();
    	}
    },
    
    getCheckBoxByValue: function(value) {
    	if (value == null) {return null;}
    	for (var i=0;i<this.__checkboxes.length;i++) {
    		if (this.__checkboxes[i].getModel() == value) {
    			return this.__checkboxes[i];
    		}
    	}
    	return null;
    }
    
    
  },
  
  destruct : function()
  {
    
  }
});